<?php

load_helper('html');
assets::instance()
	->js( "list" )
	->css( "list" );

$header = '';
foreach( $_header as $row )
{
	$header .= '<tr>';

	for($d=0;$d<$max_depth;$d++):
		$header .= '<td><!-- --></td>';
	endfor;

	foreach( $row as $cell )
	{
		$th = array( );
		foreach( array( 'class', 'title', 'width' ) as $k )
			if( isset( $cell[$k] ) )
				$th[$k] = $cell[$k];
		
		$th['title'] = isset( $th['title'] ) ? $th['title'] : $cell['text'];
		
		if( @$cell['columns'] > 1 )
			$th['colspan'] = $cell['columns'];
		if( @$cell['rows'] > 1 )
			$th['rowspan'] = $cell['rows'];
		
		$th['html'] = $cell['text'];
		
		if( @$cell['href'] )
			$th['html'] = sprintf( '<a href="%s" title="%s">%s</a>', html_escape( $cell['href'] ), html_escape( $th['title'] ), $th['html'] );
		
		$header .= html_element( 'th', $th );
	}
	$header .= '</tr>';
}

if( !isset( $id ) )
	$id = 'tablelist_'.rand( 100, 999 );

$table_props = 'cellpadding="0" cellspacing="0"';
foreach( array( 'id', 'class', 'width' ) as $k )
	if( isset( $$k ) )
		$table_props .= sprintf( ' %s="%s"', $k, html_escape( $$k ) );

?>
<table <?php echo $table_props; ?>>
<?php if( $headtype & 1 || isset($sky) ): ?>
<thead>
	<?php if( isset($sky) ): ?>
	<tr class="tablesky"><th colspan="<?php echo $_columns; ?>"><?php echo $sky; ?></th></tr>
	<?php endif; ?>
	<?php if( $headtype & 1 ) echo $header; ?>
</thead>
<?php endif; ?>
<tbody>
<?php
foreach( $from as $rowkey => $i ):
	if (empty($rowkey)) $rowkey = rand(10000,99999);
	$row = $i['row'];
	$rowid = $id.'_'.$rowkey;
	$rowclass = is_callable($row_class_callback) ? call_user_func($row_class_callback,$row,$i) : '';
	$d = 0;

	?><tr class="<?=html_cycle("odd,even")?> type-<?=$i['type']?> depth-<?=$i['depth']?> <?=$rowclass?>" id="<?=$rowid?>" rel="<?=@$i['parent']?>" <?php echo $i['type']=='group' ? '' : ' style="display:none;"'; ?>><?php

	//for($d=0;$d<$i['depth'];$d++):
	if($i['depth']>0):
		?><td colspan="<?=$i['depth']?>"><!-- --></td><?php
	endif;
	//endfor;

	switch($i['type']):
	case 'group':

		$j = 0; $plus = 0;
		foreach( $row as $k => $v ):
			$cellid = $rowid.'_'.$k;
			if( empty($v) && $plus >= 0 ) {
				$plus++;
				continue;
			}
			if( $i['group'] == $k ):

				?><td colspan="<?=$max_depth-$i['depth']+1+$plus?>" id="<?=$cellid?>"><?=$v?></td><?php
				$plus = -1;

			elseif( $j < $_columns ):

				?><td id="<?=$cellid?>"><?=$v?></td><?php

			endif;
			$j++;
		endforeach;

		for( ; $i<$_columns; $i++ ) echo '<td>'.$spacer.'</td>';

	break;

	default:

		$j = 0;
		foreach( $row as $k => $v ):
			$cellid = $rowid.'_'.$k;
			if( $j < $_columns ):

				?><td id="<?=$cellid?>"><?=$v?></td><?php

			endif;
			$j++;
		endforeach;

		for( ; $j<$_columns; $j++ ) echo '<td>'.$spacer.'</td>';

	endswitch;
	?></tr><?php

endforeach;
?>
</tbody>
<?php if( $headtype & 2 ) echo '<tfoot>'.$header.'</tfoot>'; ?>
</table>

